*------------------------------------------------------------------------------*
*																			   *
*							Communication								   	   *
*																			   *
*------------------------------------------------------------------------------*


/* Robustness check:
check if separation in political blocks is
indeed accurrate: Here: information favourable for
böock partner is coded as incongruent */


* Project: "Voting in Context" Book
* Title: "Always late? Stability and change in individuals‘ time of vote decisions"
* Author: Maria Preißinger
* Date: October 13, 2015

*-----------------------------------
* frequency talks
*-----------------------------------

forvalues num=1/7 {	
gen a_talk`num' = a_kp`num'_1930 if  a_kp`num'_1930~=99	
lab var a_talk`num' "Political talk" 	
}

egen b_talk1 = rowmax(b_kp1_1931a-b_kp1_1931e)
replace b_talk1 = . if b_talk1 < 0
replace b_talk1 = b_talk1-1
list b_kp1_1931? b_talk1

forvalues num=2/7 {
egen b_talk`num' = rowmax(b_kp`num'_1932a-b_kp`num'_1932e)
replace b_talk`num' = . if b_talk`num' < 0
replace b_talk`num' = b_talk`num'-1
lab var b_talk`num' "Political talk"
}

* Dummy: No talk vs. some talk
foreach year in a b {
foreach num of numlist 2/7 {
recode `year'_talk`num' (0=0) (1/7=1) (else=.), gen(`year'_dtalk`num')
}
}

* cummulate preceding days of talk
foreach year in a b {
foreach num of numlist 2/7 {
egen `year'_paneltalk`num' = rowtotal(`year'_talk2-`year'_talk`num'), missing
replace `year'_paneltalk`num' = . if `year'_talk`num'==.
}
}


tab b_talk2 b_dtalk2, m

						
*-------------------------------------------
* frequency public TV	
*--------------------------------------------

/* In the campaign panel 13 asked with slider. Sliders are preset on 0. Problem: 
If someone just continues, that might mean that she/he did not watsh any news or 
that she/he does not want to give an answer. In these cases in 2013 the respondents 
were inquired what actually applied for the respondent and in almost all cases it meant
0 days received. In 2009, frequency of reception was asked by using bullet points,
assumption: no answer means = 0. */


fre ?_kp2_1680b  

foreach num of numlist 2/7 {
gen a_heute`num' = a_kp`num'_1680b-1
replace a_heute`num' = 0 if a_kp`num'_1680b==99
replace a_heute`num' = 0 if a_kp`num'_1680b==100
gen a_tagesschau`num' = a_kp`num'_1680a-1
replace a_tagesschau`num' = 0 if a_kp`num'_1680a==99
replace a_tagesschau`num' = 0 if a_kp`num'_1680a==100
egen a_oeff`num' = rowtotal(a_heute`num' a_tagesschau`num'), missing
replace a_oeff`num' = 7 if a_oeff`num'>7 & a_oeff`num'~=.
}


foreach num of numlist 2/7 {
gen b_heute`num' = b_kp`num'_1680b-1
replace b_heute`num' = . if b_kp`num'_1680b < 0
gen b_tagesschau`num' = b_kp`num'_1680a-1
replace b_tagesschau`num' = . if b_kp`num'_1680a < 0 
egen b_oeff`num' = rowtotal(b_heute`num' b_tagesschau`num'), missing
replace b_oeff`num' = 7 if b_oeff`num'>7 & b_oeff`num'~=.
}


order a_oeff? b_oeff?, last  //bring it into correct order

* cumulate previous reception frequencies 
foreach year in a b {
foreach num of numlist 2/7 {
egen `year'_paneloeff`num' = rowtotal(`year'_oeff2-`year'_oeff`num'), missing
replace `year'_paneloeff`num' = . if `year'_oeff`num'==.
}
}


*---------------------------------------
* frequency daily newspapers
*---------------------------------------

/* in 2009, frequencies of receotion were asked by using a matrix with bullet points.
A lot of respondents only filled in parts of the matrix. Assumption: no answer means = 0 days.
In the campaign panel 2013 asked with slider. Sliders are preset on 0. Problem: 
If someone just continues, that might mean that she/he did not watsh any news or 
that she/he does not want to give an answer. In these cases in 2013 the respondents 
were inquired what actually applied for the respondent and in almost all cases it meant
0 days received. */
	
	
	
foreach year in a b {
foreach l in b c d e f g {	
foreach num of numlist 2/7 {
gen `year'_help`l'`num' = `year'_kp`num'_1660`l'-1	
replace `year'_help`l'`num' = 0 if `year'_kp`num'_1660`l'==100 | `year'_kp`num'_1660`l'==99 
replace `year'_help`l'`num' = . if  `year'_kp`num'_1660`l' < 0
}
}
}

tab a_helpb2 a_kp2_1660b, m

foreach year in a b {
foreach num of numlist 2/7 {
egen `year'_paper`num' = rowtotal(`year'_help?`num'), missing
replace `year'_paper`num' = 7 if `year'_paper`num' > 7 & `year'_paper`num'~=.
}
}

* cumulate previous frequency of reception
foreach year in a b {
foreach num of numlist 2/7 {
egen `year'_panelpaper`num' = rowtotal(`year'_paper2-`year'_paper`num'), missing
replace `year'_panelpaper`num' = . if `year'_paper`num'==.
}
}


* hypiene of the data: delete useless stuff
drop *help*
	
	
*----------------------------------------------
* frequency party contact
*----------------------------------------------	
	
* Counts per Party
foreach welle of numlist 2/6 {
egen a_countcontactcdu`welle' = anycount(a_kp`welle'_420a_1 ///
	a_kp`welle'_420d_1 a_kp`welle'_420e_1  ///
	a_kp`welle'_420g_1 a_kp`welle'_420h_1 a_kp`welle'_420k_1), values(1)
replace a_countcontactcdu`welle' = a_countcontactcdu`welle'+1 ///
	if a_kp`welle'_420b_1==1 | a_kp`welle'_420c_1==1  //flyer or Mail (2013 surved together)
	
egen a_countcontactspd`welle' = anycount(a_kp`welle'_420a_2 ///
	a_kp`welle'_420d_2 a_kp`welle'_420e_2  ///
	a_kp`welle'_420g_2 a_kp`welle'_420h_2 a_kp`welle'_420k_2), values(1)
replace a_countcontactspd`welle' = 	a_countcontactspd`welle'+1 ///
	if a_kp`welle'_420b_2==1 | a_kp`welle'_420c_2==1
	
egen a_countcontactfdp`welle' = anycount(a_kp`welle'_420a_3 ///
	a_kp`welle'_420d_3 a_kp`welle'_420e_3  ///
	a_kp`welle'_420g_3 a_kp`welle'_420h_3 a_kp`welle'_420k_3), values(1)
replace a_countcontactfdp`welle' = 	a_countcontactfdp`welle'+1 ///
	if a_kp`welle'_420b_3==1 | a_kp`welle'_420c_3==1

egen a_countcontactgru`welle' = anycount(a_kp`welle'_420a_4 ///
	a_kp`welle'_420d_4 a_kp`welle'_420e_4  ///
	a_kp`welle'_420g_4 a_kp`welle'_420h_4 a_kp`welle'_420k_4), values(1)
replace a_countcontactgru`welle' = 	a_countcontactgru`welle'+1 ///
	if a_kp`welle'_420b_4==1 | a_kp`welle'_420c_4==1

egen a_countcontactlink`welle' = anycount(a_kp`welle'_420a_5 ///
	a_kp`welle'_420d_5 a_kp`welle'_420e_5  ///
	a_kp`welle'_420g_5 a_kp`welle'_420h_5 a_kp`welle'_420k_5), values(1)
replace a_countcontactlink`welle' = a_countcontactlink`welle'+1 ///
	if a_kp`welle'_420b_5==1 | a_kp`welle'_420c_5==1
	
}


foreach welle of numlist 2/6 {
foreach party in cdu spd fdp gru link {
replace a_countcontact`party'`welle' = . if a_kp`welle'_400>2
}
}

foreach welle of numlist 2/6 {
egen b_countcontactcdu`welle' = anycount(b_kp`welle'_421ka b_kp`welle'_421ga ///
	b_kp`welle'_421aa b_kp`welle'_421da b_kp`welle'_421ea ///
	b_kp`welle'_421ha b_kp`welle'_421ba), values(1)
egen b_countcontactspd`welle' = anycount(b_kp`welle'_421kc b_kp`welle'_421gc ///
	b_kp`welle'_421ac b_kp`welle'_421dc b_kp`welle'_421ec ///
	b_kp`welle'_421hc b_kp`welle'_421bc), values(1)
egen b_countcontactfdp`welle' = anycount(b_kp`welle'_421kd b_kp`welle'_421gd ///
	b_kp`welle'_421ad b_kp`welle'_421dd b_kp`welle'_421ed ///
	b_kp`welle'_421hd b_kp`welle'_421bd), values(1)
egen b_countcontactgru`welle' = anycount(b_kp`welle'_421ke b_kp`welle'_421ge ///
	b_kp`welle'_421ae b_kp`welle'_421de b_kp`welle'_421ee ///
	b_kp`welle'_421he b_kp`welle'_421be), values(1)
egen b_countcontactlink`welle' = anycount(b_kp`welle'_421kf b_kp`welle'_421gf ///
	b_kp`welle'_421af b_kp`welle'_421df b_kp`welle'_421ef ///
	b_kp`welle'_421hf b_kp`welle'_421bf), values(1)
}


foreach welle of numlist 2/6 {
foreach party in cdu spd fdp gru link {
replace b_countcontact`party'`welle' = . if b_kp`welle'_421z<0 | b_kp`welle'_421y<0 | b_kp`welle'_421z==.
}
}


* general Count independent from party
foreach year in a b {
foreach welle of numlist 2/6 {
egen `year'_countcontact`welle' = ///
	rowtotal(`year'_countcontactcdu`welle'-`year'_countcontactlink`welle'), missing
replace `year'_countcontact`welle'	= 5 if `year'_countcontact`welle'>5 & `year'_countcontact`welle'~=.
}
}

//list b_countcontactcdu2-b_countcontactlink2 b_countcontact2

fsum a_countcontact* if !missing(a_countcontact2, a_countcontact3, ///
	a_countcontact4, a_countcontact5, a_countcontact6)	
	
* dummy: contact at any point in time?
foreach year in a b {
foreach welle of numlist 2/6 {
recode `year'_countcontact`welle' (0=0) (.=.) (else=1), ///
	gen(`year'_dcontact`welle')
}
}

tab b_countcontact3 b_dcontact3, m

* panel general count cumulated
foreach year in a b {
foreach welle of numlist 2/6 {
egen `year'_panelcountcontact`welle' = ///
	rowtotal(`year'_countcontact2-`year'_countcontact`welle'), missing
}
}

//fre ?_panelcountcontact?	

*--------------------------------------------------------------
* is party contact congruent with party of final 
* vote decision? yes/no
*---------------------------------------------------------------

/* Dummy: year/no + cumulate */

* Contact for party as dummy
foreach var of varlist ?_countcontactcdu? ?_countcontactspd? ///
	?_countcontactfdp? ?_countcontactgru? ?_countcontactlink? {
gen `var'd = `var'
replace `var'd = 1 if `var'>0 & `var'!=.
}


fre a_countcontactgru?

* Cumulate party contact dummies
foreach year in a b {
foreach welle of numlist 2/6 {
foreach party in cdu spd fdp gru link {
egen `year'_panelcountcontact`party'`welle' = ///
	rowtotal(`year'_countcontact`party'2d-`year'_countcontact`party'`welle'd), missing
replace `year'_panelcountcontact`party'`welle' = . if `year'_countcontact`party'`welle'd==.
}
}
}

* Party contact congruent with final vote choice?
foreach year in a b {
foreach welle of numlist 2/6 {
gen `year'_panelcongcontact`welle' = .
replace `year'_panelcongcontact`welle' = `year'_panelcountcontactcdu`welle' ///
	if `year'_wabsfull7==1
replace `year'_panelcongcontact`welle' = `year'_panelcountcontactspd`welle' ///
	if `year'_wabsfull7==4
replace `year'_panelcongcontact`welle' = `year'_panelcountcontactfdp`welle' ///
	if `year'_wabsfull7==5
replace `year'_panelcongcontact`welle' = `year'_panelcountcontactgru`welle' ///
	if `year'_wabsfull7==6
replace `year'_panelcongcontact`welle' = `year'_panelcountcontactlink`welle' ///
	if `year'_wabsfull7==7
}
}

* Data hygiene:
drop ?_countcontact*d 
drop a_panelcountcontactcdu2-b_panelcountcontactlink6

	

*---------------------------------------------------------------------------
* Is conversational talk congruent with final vote decision?
*---------------------------------------------------------------------------


fre a_kp6_1970
lab define wabsgp ///
		   1   "CDU/CSU" ///
           4   "SPD" ///
           5   "FDP" ///
           6   "GRUENE" ///
           7   "DIE LINKE" ///
         801   "andere Partei" ///
         888   "weiß nicht/GP hat sich noch nicht entschieden" ///
         999   "GP wird nicht wählen", replace
		 
foreach year in a b {		 
foreach num of numlist 1/7 {
capture recode `year'_kp`num'_1970 (1=1) (4=4) (5=5) (6=6) (7=7) (801=801) ///
	(8=801) (991 998 -98=888) (995 -85=999) (996 999 997 100=.) (else=.), ///
	gen(`year'_wabsfull_gp`num')
}
}

tab a_kp5_1970 a_wabsfull_gp5, m
//tab b_wabsfull_gp1  //nothing = everything alright
tab b_kp5_1970 b_wabsfull_gp5, m
lab val *wabsfull_gp* wabsgp

fre ?_wabsfull_gp?

* Control
tab a_wabsfull_gp2 a_kp2_1970, m 
tab b_wabsfull_gp2 b_kp2_1970, m 
fre a_kp2_1970


* dummy vote intention conversational partner for certain parties
foreach year in a  {
foreach welle of numlist 1/7 {
gen `year'_gesprcdu`welle' = 1 if `year'_wabsfull_gp`welle'==1
replace `year'_gesprcdu`welle' = 0 if `year'_wabsfull_gp`welle'>1 & ///
	`year'_wabsfull_gp`welle'<=999
replace `year'_gesprcdu`welle' = 0 if `year'_kp`welle'_1930==0
}
}


foreach year in a  {
foreach welle of numlist 1/7 {
gen `year'_gesprspd`welle' = 1 if `year'_wabsfull_gp`welle'==4
replace `year'_gesprspd`welle' = 0 if `year'_wabsfull_gp`welle'~=4 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprspd`welle' = 0 if `year'_kp`welle'_1930==0
}
}


foreach year in a  {
foreach welle of numlist 1/7 {
gen `year'_gesprfdp`welle' = 1 if `year'_wabsfull_gp`welle'==5
replace `year'_gesprfdp`welle' = 0 if `year'_wabsfull_gp`welle'~=5 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprfdp`welle' = 0 if `year'_kp`welle'_1930==0
}
}

foreach year in a  {
foreach welle of numlist 1/7 {	
gen `year'_gesprgru`welle' = 1 if `year'_wabsfull_gp`welle'==6
replace `year'_gesprgru`welle' = 0 if `year'_wabsfull_gp`welle'~=6 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprgru`welle' = 0 if `year'_kp`welle'_1930==0
}
}

foreach year in a  {
foreach welle of numlist 1/7 {
gen `year'_gesprlink`welle' = 1 if `year'_wabsfull_gp`welle'==7
replace `year'_gesprlink`welle' = 0 if `year'_wabsfull_gp`welle'~=7 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprlink`welle' = 0 if `year'_kp`welle'_1930==0
}
}

foreach year in b  {
foreach welle of numlist 2/7 {
gen `year'_gesprcdu`welle' = 1 if `year'_wabsfull_gp`welle'==1
replace `year'_gesprcdu`welle' = 0 if `year'_wabsfull_gp`welle'>1 & ///
	`year'_wabsfull_gp`welle'<=999
replace `year'_gesprcdu`welle' = 0 if `year'_kp`welle'_1932f==1 
}
}



foreach year in b  {
foreach welle of numlist 2/7 {
gen `year'_gesprspd`welle' = 1 if `year'_wabsfull_gp`welle'==4
replace `year'_gesprspd`welle' = 0 if `year'_wabsfull_gp`welle'~=4 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprspd`welle' = 0 if `year'_kp`welle'_1932f==1 
}
}



foreach year in b  {
foreach welle of numlist 2/7 {
gen `year'_gesprfdp`welle' = 1 if `year'_wabsfull_gp`welle'==5
replace `year'_gesprfdp`welle' = 0 if `year'_wabsfull_gp`welle'~=5 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprfdp`welle' = 0 if `year'_kp`welle'_1932f==1 
}
}


foreach year in b  {
foreach welle of numlist 2/7 {	
gen `year'_gesprgru`welle' = 1 if `year'_wabsfull_gp`welle'==6
replace `year'_gesprgru`welle' = 0 if `year'_wabsfull_gp`welle'~=6 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprgru`welle' = 0 if `year'_kp`welle'_1932f==1 
}
}


foreach year in b  {
foreach welle of numlist 2/7 {
gen `year'_gesprlink`welle' = 1 if `year'_wabsfull_gp`welle'==7
replace `year'_gesprlink`welle' = 0 if `year'_wabsfull_gp`welle'~=7 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprlink`welle' = 0 if `year'_kp`welle'_1932f==1 
}
}

tab b_gesprlink4 b_kp4_1932f, m


* talks cumulated 
foreach year in a b {
foreach party in cdu spd fdp gru link {
foreach welle of numlist 2/7 {
egen `year'_panelgespr`party'`welle' = ///
	rowtotal(`year'_gespr`party'2-`year'_gespr`party'`welle'), missing  
replace `year'_panelgespr`party'`welle' = . ///
		if `year'_gespr`party'`welle'==.
 }
 }
 }
 
 
* congruent talks by final vote decision
foreach year in a b {
foreach num of numlist 2/7 {
gen `year'_panelcongtalk`num' = .
replace `year'_panelcongtalk`num' = `year'_panelgesprcdu`num' if `year'_wabsfull7==1
replace `year'_panelcongtalk`num' = `year'_panelgesprspd`num' if `year'_wabsfull7==4
replace `year'_panelcongtalk`num' = `year'_panelgesprfdp`num' if `year'_wabsfull7==5
replace `year'_panelcongtalk`num' = `year'_panelgesprgru`num' if `year'_wabsfull7==6
replace `year'_panelcongtalk`num' = `year'_panelgesprlink`num' if `year'_wabsfull7==7
}
}

* hygiene of data
drop a_wabsfull_gp1-b_panelgesprlink7


* control

/* If acumulation worked, a variable in one wave cannot be lager that variable of
preceding wave */

foreach year in a b {
foreach num of numlist 2/6 {
local i = `num' +1
fre `year'_panelcongtalk? if ///
	`year'_panelcongtalk`num'>`year'_panelcongtalk`i' ///
	& `year'_panelcongtalk`num'~=.
}
}

/* it works! */

*+++++++++++++++++++++++++++++++++++++++++++++++++++++++
* partisan bias of TV news-messages
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++

*-------------------------------------
* date of the interview
*-------------------------------------

/* existing variable is a string. Save in a reasonable format. */

//mvdecode b_kp?_datetime, mv(.e) //not working


foreach year in a b {
foreach wave of numlist 1/7  {
gen str `year'_help`wave' = substr(`year'_kp`wave'_datetime, 1, 10) ///
	if `year'_kp`wave'_datetime~=".e nicht teilgenommen" //erst mal Uhrzeit abschneiden und Missingcode ignorieren
gen `year'_year`wave's = substr(`year'_help`wave', 1, 4) if `year'_kp`wave'_datetime~="-95 nicht teilgenommen"
destring `year'_year`wave's, gen(`year'_year`wave')   //in numerische Werte umwandeln
gen `year'_month`wave's = substr(`year'_help`wave', 6, 2) if `year'_kp`wave'_datetime~="-95 nicht teilgenommen"
destring `year'_month`wave's, gen(`year'_month`wave')
gen `year'_day`wave's = substr(`year'_help`wave', 9, 2) if `year'_kp`wave'_datetime~="-95 nicht teilgenommen"
destring `year'_day`wave's, gen(`year'_day`wave')
gen `year'_date`wave' = ///
	mdy(`year'_month`wave', `year'_day`wave', `year'_year`wave')  //Stata-Datumsformat
format `year'_date`wave' %d  //leserlich machen
drop `year'_help`wave'
}
}

drop ?_day* ?_month* 
drop ?_year? ?_year??



*------------------------------------------------------
* Matching media bias 2009
*-----------------------------------------------------


/* Do we need to match each wave seperately?
Idea: copy all date-variables for each wave in the tone of conversation-dataset and add prefix "a_"
Attention: _merge = 2 are Information about tones of coverage for days that noone has been 
interviewed, these observations need to be deleted. */

foreach num of numlist 2/6 {
merge m:1 a_date`num' using ///
	"tvcontent2009.dta", ///
		keepusing(a_finalton*`num')
tab a_w`num' _merge, m


drop if _merge==2

drop _merge
}

fsum a_finalton*


*-----------------------------------------------------
* Matching media bias 2013
*-------------------------------------------------------

/*
Attention: _merge = 2 are Information about tone of conversation for days that noone has been 
interviewed in the campaign panel, these observations need to be deleted, so that 
each row in the dataset contains one respondent. 
*/


gen b_w1 = substr(b_teilnahme, 1, 1)
gen b_w2 = substr(b_teilnahme, 5, 1)
gen b_w3 = substr(b_teilnahme, 9, 1)
gen b_w4 = substr(b_teilnahme, 13, 1)
gen b_w5 = substr(b_teilnahme, 17, 1)
gen b_w6 = substr(b_teilnahme, 21, 1)
gen b_w7 = substr(b_teilnahme, 25, 1)

destring b_w?, replace

foreach num of numlist 2/6 {
merge m:1 b_date`num' using ///
	"tvcontent2013.dta", ///
		keepusing(b_finalton*`num')
tab b_w`num' _merge, m
drop if _merge==2
drop _merge
}

fsum b_finalton*


*-------------------------------------------------------
* create final variables: media coverage favourable of 
* the party that respondent actually voted for?
*---------------------------------------------------------

rename ?_tagesschau? ?_ard?
rename ?_heute? ?_zdf?


/* Of course, only people who received the medium of concern, get a congruent
tone of conversation, otherwise = 0 */

* congruent vs. incongruent/neutral
foreach year in a b {
foreach s in ard zdf {
foreach wave of numlist 2/6 {
gen `year'_cong`s'`wave' = 0
replace `year'_cong`s'`wave' = 1 if `year'_finaltoncdu`s'`wave'>0 & `year'_finaltoncdu`s'`wave'!=. ///
	& `year'_wabsfull7==1
replace `year'_cong`s'`wave' = 1 if `year'_finaltonspd`s'`wave'>0 & `year'_finaltonspd`s'`wave'!=. ///
	&`year'_wabsfull7==4
replace `year'_cong`s'`wave' = 1 if `year'_finaltonfdp`s'`wave'>0 & `year'_finaltonfdp`s'`wave'!=. ///
	&`year'_wabsfull7==5
replace `year'_cong`s'`wave' = 1 if `year'_finaltongru`s'`wave'>0 & `year'_finaltongru`s'`wave'!=. ///
	&`year'_wabsfull7==6
replace `year'_cong`s'`wave' = 1 if `year'_finaltonlink`s'`wave'>0 & `year'_finaltonlink`s'`wave'!=. ///
	&`year'_wabsfull7==7
replace `year'_cong`s'`wave' = 0 if `year'_`s'`wave'==0 
replace `year'_cong`s'`wave' = . if `year'_`s'`wave'==. | `year'_wabsfull7>7
}
}
}


bysort b_wabsfull7: fsum b_congard*
bysort b_wabsfull7: fsum b_congzdf*

bysort a_wabsfull7: fsum a_congard*


bysort a_wabsfull7: fsum a_congard*

/* extreme gaps */

* cumulate
foreach year in a b {
foreach s in ard zdf {
foreach wave of numlist 2/6 {
egen `year'_panelcong`s'`wave' = ///
	rowtotal(`year'_cong`s'2-`year'_cong`s'`wave'), missing
replace `year'_panelcong`s'`wave' = . if `year'_cong`s'`wave'==.
}
}
}

list a_congard2-a_congard6 a_panelcongard6

* hygiene of the data
drop ?_cong*
drop ?_finalton*


*--------------------------------------------
* TV-debate
*-----------------------------------------------

*+++++++++++++++++++++++
* Reception TV debate
* +++++++++++++++++++++++

lookfor duell

fre ?_kp?_1800

recode a_kp6_1800 (1 2=1) (3=0) (else=.), gen(a_tvduell6)
recode b_kp5_1800 (1 2=1) (3=0) (else=.), gen(b_tvduell5)

* replace with 0 for waves before the TV debate
foreach num of numlist 2 3 4 5 {
gen a_tvduell`num' = 0 if wkp0913==2 | wkp0913==3
}

foreach num of numlist 2 3 4 {
gen b_tvduell`num' = 0 if wkp0913==1 | wkp0913==3
}

* replace wave 6 2013 with wave 5
/* we only need up to wave 6, that's why we do not need to replace anything for 2009 */
gen b_tvduell6 = b_tvduell5 if wkp0913==1 | wkp0913==3

*++++++++++++++++++++++++++++++++++
* Content TV-debate 
*++++++++++++++++++++++++++++++++++

lookfor duell

tab b_kp6_1810 b_kp6_1820 

* Difference of performance Merkel and challenger TV-debate
gen b_merkelbesser = (6-b_kp5_1810) - (6-b_kp5_1820) ///
	if b_kp5_1810>0 & b_kp5_1820>0
list b_kp6_1810 b_kp6_1820 b_merkelbesser

fre b_merkelbesser

gen a_merkelbesser = (6-a_kp6_1810) - (6-a_kp6_1820) ///
	if a_kp6_1810<=5 & a_kp6_1820<=5
	
fre a_merkelbesser	

* assessment performance congruent to final vote decision?
gen a_panelcongtvduell6 = 0
replace a_panelcongtvduell6 = 1 if a_merkelbesser>0 & a_wabsfull7==1 
replace a_panelcongtvduell6 = 1 if a_merkelbesser<0 & a_wabsfull7==4 	
replace a_panelcongtvduell6 = . if a_wabsfull7>7
replace a_panelcongtvduell6 = . if a_merkelbesser==.
replace a_panelcongtvduell6 = 0 if a_tvduell6==0 //if TV-debate not seen,
												 //could not receive congruent message
	
foreach num of numlist 2/5 {
gen a_panelcongtvduell`num' = a_tvduell`num' if wkp0913==2 | wkp0913==3
}
	

gen b_panelcongtvduell5 = 0
replace b_panelcongtvduell5 = 1 if b_merkelbesser>0 & b_wabsfull7==1
replace b_panelcongtvduell5 = 1 if b_merkelbesser<0 & b_wabsfull7==4 	
replace b_panelcongtvduell5 = . if b_wabsfull7>7
replace b_panelcongtvduell5 = . if b_merkelbesser==.
replace b_panelcongtvduell5 = 0 if b_tvduell5==0

gen b_panelcongtvduell6 = b_panelcongtvduell5 

foreach num of numlist 2/4 {
gen b_panelcongtvduell`num' = b_tvduell`num' if wkp0913==1 | wkp0913==3
}


fsum ?_panelcongtv*


*-----------------------------------------------------						
* time of critical contact (streams of communication)
*------------------------------------------------------

fre a_panelcongtalk2 if !missing(a_panelcongtalk2, a_panelcongtalk3, ///
	a_panelcongtalk4, a_panelcongtalk5, a_panelcongtalk6)
	

foreach year in a b {
foreach var in talk contact ard zdf {	
gen `year'_time`var' = 0 //should remain at the end for non congruent talks
replace `year'_time`var' = 2 if `year'_panelcong`var'2==1
replace `year'_time`var' = 3 if `year'_panelcong`var'3==1 & `year'_panelcong`var'2==0					
replace `year'_time`var' = 4 if `year'_panelcong`var'4==1 & `year'_panelcong`var'3+`year'_panelcong`var'2==0							
replace `year'_time`var' = 5 if `year'_panelcong`var'5==1 & ///
	`year'_panelcong`var'4+`year'_panelcong`var'3+`year'_panelcong`var'2==0							
replace `year'_time`var' = 6 if `year'_panelcong`var'6==1 & ///
	`year'_panelcong`var'5+`year'_panelcong`var'4+`year'_panelcong`var'3+`year'_panelcong`var'2==0		
replace `year'_time`var' = . if `year'_panelcong`var'2+`year'_panelcong`var'3+ ///
	`year'_panelcong`var'4+`year'_panelcong`var'5+`year'_panelcong`var'6==. //if there is a missing somewhere
}
}	

lab define time ///
	0 "never" ///
	2 "wave 2" ///
	3 "wave 3" ///
	4 "wave 4" ///
	5 "wave 5" ///
	6 "wave 6", replace
lab val ?_timetalk ?_timecontact ?_timeard ?_timezdf time











